﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class AutoChessMatchSucceedPageParam
    {
        public int countDownSeconds;
    }

    public class AutoChessMatchSucceedPage : OPPage
    {
        public Transform particleRoot;
        public Text countdownText;

        private UIEffect matchSuccedParticle;
        private float deltaTime;
        private int curCounter = 3;

        // Use this for initialization
        void Start()
        {
            deltaTime = 0f;
        }

        // Update is called once per frame
        void Update()
        {
            if (countdownText == null || curCounter == 0)
            {
                return;
            }

            deltaTime += Time.deltaTime;
            if (deltaTime >= 1f)
            {
                deltaTime = 0f;
                curCounter--;
                countdownText.text = curCounter.ToString();
            }
        }

        protected override void DoOpen()
        {
            AutoChess.UtilityHelper.PlayUIAudio("ArenaPK_enter");
            matchSuccedParticle = UIEffectManager.Instance.PlayEffectImmediate(UIEffectResId.EffectId_AutoChess_Match_Succeed, particleRoot);
            AutoChessMatchSucceedPageParam curMatchSucceedParams = Params as AutoChessMatchSucceedPageParam;
            if (curMatchSucceedParams != null)
            {
                if (curMatchSucceedParams.countDownSeconds <= 0)
                {
                    curCounter = Table.DataModel.TableConfig.autoChessBattleDelayTime / 1000;
                    countdownText.text = curCounter.ToString();
                }
                else
                {
                    curCounter = curMatchSucceedParams.countDownSeconds;
                    countdownText.text = curCounter.ToString();
                }
            }
        }

        protected override void DoClose(bool immediately)
        {
            base.DoClose(immediately);
            if (matchSuccedParticle != null)
            {
                UIEffectManager.Instance.ClearUIEffectByResId(UIEffectResId.EffectId_AutoChess_Match_Succeed, particleRoot);
            }
        }
    }
}
